#!/usr/bin/python
# -*- coding: utf-8 -*-
"""公共custom tags and filter"""

from django import template
from jiaju.common.constants.constant import Constant
from jiaju.model import User

register = template.Library()

@register.filter
def check_attr(attr_oten):
    return 0

@register.filter
def check_is_privileges(privilege_value , key_values):
    if not privilege_value :
        return ""
    if privilege_value % key_values == 0 :
        return "checked=\"checked\""

@register.filter
def check_is_invite_success(status):
    if status == 0 : 
        return "<font color='red'>邀请中...</font>"
    else :
        return "<font color='green'>成功</font>"

@register.filter
def check_is_clear_success(clear):
    if clear == 0 : 
        return "<font color='red'>未结算</font>"
    else :
        return "<font color='green'>已结算</font>"

@register.filter
def get_user_name(request):
    session_id = request.COOKIES.get(Constant.SESSION_KEY, None)
    username = ""
    if session_id == None :
        return ""
    users = User.load(session_id=session_id)
    if users != None:
        username = users.username
    return username+u" <a href='/admin/logout/'>退出</a>"
    
@register.filter
def get_mobile(mobile):
    if mobile == "" or mobile is None:
        return ""
    pre_mobile = mobile[0:3]
    last_mobile = mobile[7:len(mobile)]
    return pre_mobile + "****" + last_mobile

    
    